﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntitiesLayer;
using DAL.Repositories;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;


namespace DAL.Repositories
{
    internal class CuerpoCelesteRepository : IRepository<CuerpoCeleste>
    {
        private IList<IEntity> insertedList;
        private IList<IEntity> updatedList;
        private IList<IEntity> deletedList;
        
        private static Dictionary<char,Func<DataRow,int, CuerpoCeleste>> dictionario;

        internal CuerpoCelesteRepository()
        {
            insertedList = new List<IEntity>();
            updatedList = new List<IEntity>();
            deletedList = new List<IEntity>();
        }

        static CuerpoCelesteRepository()
        {
    
            dictionario = new Dictionary<char, Func<DataRow, int, CuerpoCeleste>>();
            
            dictionario.Add('E', EstrellaRepository.crearEstrella);
            dictionario.Add('S', SateliteRepository.crearSatelite);
            dictionario.Add('P', PlanetaRepository.crearPlaneta);
        }

        public void Insert(CuerpoCeleste objCeleste)
        {

            throw new NotImplementedException();

        }

        public void Update(CuerpoCeleste objCeleste)
        {
            throw new NotImplementedException();

        }

        public void Delete(CuerpoCeleste objCeleste)
        {
            throw new NotImplementedException();

        }


        public IEnumerable<CuerpoCeleste> GetAll()
        {
            
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_listarCuerposCelestes");

             foreach (DataRow dr in ds.Tables[0].Rows)
            {
                char type = Convert.ToChar(dr["TIPO"]);
                yield return dictionario[type](dr, Convert.ToInt32(dr["IDCUERPO"]));
            }

        }

        public CuerpoCeleste GetById(int id)
        {
           
            CuerpoCeleste objSatelite = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@IdCuerpo", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_consultar_cuerpo_porID");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                char type = Convert.ToChar(dr["TIPO"]);
                return dictionario[type](dr, id);
            }

            return objSatelite;
        }

      
        public void Save()
        {
            throw new NotImplementedException();

            using (TransactionScope scope = new TransactionScope())
            {

                try
                {

                    foreach (Satelite p in insertedList)
                        Insert(p);

                    foreach (Satelite p in updatedList)
                        Update(p);

                    foreach (Satelite p in deletedList)
                        Delete(p);

                    scope.Complete();
                }
                catch (TransactionAbortedException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
                catch (ApplicationException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
                finally
                {
                    Clear();
                }

            }
        }

        public void Clear()
        {
            insertedList.Clear();
            updatedList.Clear();
            deletedList.Clear();
        }
        #region Utilities
       


        #endregion Utilities
    }
}

